package com.caoustc.okhttplib.okhttp.utils

import com.caoustc.okhttplib.okhttp.Part
import java.io.UnsupportedEncodingException
import java.net.URLEncoder

object OkHttpUtils {
    fun getFullUrl(url: String?, params: List<Part?>?, urlEncoder: Boolean): String {
        val urlFull = StringBuilder()
        urlFull.append(url)
        if (urlFull.indexOf("?", 0) < 0 && params!!.size > 0) {
            urlFull.append("?")
        }
        var flag = 0
        for (part in params!!) {
            var key = part?.key
            var value = part?.value
            if (urlEncoder) {
                try {
                    key = URLEncoder.encode(key, "UTF-8")
                    value = URLEncoder.encode(value, "UTF-8")
                } catch (e: UnsupportedEncodingException) {
                    e.printStackTrace()
                }
            }
            urlFull.append(key).append("=").append(value)
            if (++flag != params.size) {
                urlFull.append("&")
            }
        }
        return urlFull.toString()
    }
}